package cn.dengta.webapp.trade.dto;

import java.math.BigDecimal;
import java.util.Date;
import javax.annotation.Nullable;

import cn.dengta.webapp.trade.model.TradeType;
import lombok.*;
import lombok.experimental.Accessors;

@Getter
@Setter
@Accessors(chain = true)
public class UserStatChangeDTO {

	/** 发起方用户ID */
	Long userId;
	/** 交易类型 */
	TradeType tradeType;
	/** 接收方用户ID */
	Long toUserId;

	/** 付费方用户ID */
	Long payUserId;
	/** 当前时间 */
	Date now;
	/** 消费的金币数 或 充值金额（单位：分） */
	Integer amount;
	BigDecimal incomeIntegral;

	/** 音视频的时长（单位：秒） */
	Integer callTime;

	public UserStatChangeDTO init(Long fromUserId, TradeType tradeType, @Nullable Long toUserId, Long payUserId, Date now,
	                              @Nullable Integer amount, @Nullable BigDecimal incomeIntegral) {
		this.userId = fromUserId;
		this.tradeType = tradeType;
		this.toUserId = toUserId;
		this.payUserId = payUserId;
		this.now = now;
		this.amount = amount;
		this.incomeIntegral = incomeIntegral;
		return this;
	}

	@Nullable
	public static UserStatChangeDTO of(Long fromUserId, TradeType tradeType, @Nullable Long toUserId, Long payUserId, Date now,
	                                   @Nullable Integer amount, @Nullable BigDecimal incomeIntegral) {
		switch (tradeType) {
			case COIN_RECHARGE:
			case COIN_FOR_GREET:
			case COIN_FOR_MSG:
			case COIN_FOR_VIDEO:
			case COIN_FOR_AUDIO:
			case COIN_FOR_GIFT:
				return new UserStatChangeDTO().init(fromUserId, tradeType, toUserId, payUserId, now, amount, incomeIntegral);
		}
		return null;
	}

	public Long getIncomeUserId() {
		return userId.equals(payUserId) ? toUserId : userId;
	}

	@NonNull
	public Integer getCallTime() {
		return callTime == null ? 0 : callTime;
	}

	/** 通话次数（仅限音视频通话使用） */
	public int getTimes() {
		return callTime == null ? 0 : 1;
	}

	/**
	 * 获取支付金额（单位：分）
	 */
	@NonNull
	public Integer getPayAmount() {
		return amount;
	}

	@NonNull
	public Integer getCoin() {
		return amount == null ? 0 : amount;
	}

	/**
	 * 获取收益的积分额度（以 0.01 积分为单位）
	 */
	@NonNull
	public Integer getIncome() {
		return incomeIntegral == null ? 0 : incomeIntegral.movePointRight(2).intValueExact();
	}

}
